package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 八百方
 * @author tngou@tngou.net
 *
 */
public class COM_800pharm  extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(COM_800pharm.class);
	private String url="http://www.800pharm.com/shop/product-100068-${}.html";
	
	

	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null||doc.getElementById("introduction")==null)return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		String[] uns = url.toString().split("-");
		json.put("un",uns[uns.length-1].replaceAll(".html", "-").split("-")[0]);
		//标签
		Elements procrumbs = doc.getElementsByClass("navAdd");
		if(procrumbs.first()!=null)
		{
			Elements as = procrumbs.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 1; i < as.size(); i++) {
				list.add(as.get(i).text().replaceAll(">", ""));
			}				
			json.put("tag", list);
		}
		//图片
		Elements items = doc.getElementsByClass("mini_list");
		if(items.first()!=null)
		{
			Elements imgs = items.first().getElementsByTag("img");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				String src = e.attr("src");
				if(StringUtil.isNotEmpty(src))
				{
					list.add(src);
				}
			});
			json.put("picture", list);
		}
		//价格
		Elements price = doc.getElementsByClass("price_shang");
		if(price.first()!=null)
		{	
			String value = price.first().text();		
			json.put("price", getPrice(value));
			
		}
		//类型
		Elements main_mid = doc.getElementsByClass("main_mid");
		if(main_mid.first()!=null) {
		Elements h1 = main_mid.first().getElementsByTag("h1");
		if(h1.first()!=null) {
			Elements span = h1.first().getElementsByTag("span");
			if(span.first()!=null)
			{
				String s = span.first().attr("class");
				if(StringUtil.isNotEmpty(s))
				{
					if(s.toUpperCase().equals("RX"))
					{
						json.put("ptype", Ptype.RX处方药.ordinal());
					}else if(s.toUpperCase().equals("OTC"))
					{
						json.put("ptype", Ptype.OTC非处方甲类.ordinal());
					}
				}
			}
		}
		}
		//说明书
		Element instruction = doc.getElementById("introduction");
		if(instruction!=null)
		{
			JSONObject instructions = instructions_tr_td_td(instruction);
			Elements pro_msg = doc.getElementsByClass("pro_msg");
			if(pro_msg.first()!=null)
				instructions = instructions(pro_msg.first(),instructions);
			json.put("instructions",instructions );
			
		}
		return json;
	}
	private JSONObject instructions(Element element, JSONObject json) {
		Element p = element.getElementsMatchingOwnText("批准文号").first();
	
		if(p!=null) {
			String s=StringUtil.split(p.parent().text(),"|")[0];
			String[] es = StringUtil.split(s, "：");
			if(es.length==2) {
				String key=StringUtil.cleanKey(es[0]);
				String value = StringUtil.RemoveEmpty(es[1]);	
				json.put(key, value);
			}
		}
		
		return  name_number(json);
	}
	

}
